﻿import A2.utils.uMC;
import mx.utils.Delegate;
class A2.io.Rat {
	// [置顶]
	// AS2.MoE.follow(this)
	function Rat() {
	}
	static function follow(scope:MovieClip,mc:MovieClip, obj:Object):Void {
		var ctrl = uMC.newMC();
		ctrl.mc = mc;
		ctrl.xo = mc._x -mc._parent._xmouse;
		ctrl.yo = mc._y -mc._parent._ymouse;
		ctrl.onPP =  (obj.onPP==null)?null: obj.onPP;
		ctrl.scope = scope;
		//
		mc.follow_ctrl = ctrl;
		//
		if (obj.onPP != null) {
			
			ctrl.onEnterFrame = function() {
				if(!_root.isOut){
					this.mc._x  = this.mc._parent._xmouse + this.xo;
					this.mc._y  = this.mc._parent._ymouse + this.yo;	
					Delegate.create(this.scope, this.onPP)( { mc:this.mc } );
				}
			};
		}else {
			ctrl.onEnterFrame = function() {
				if(!_root.isOut){
					this.mc._x  = this.mc._parent._xmouse + this.xo;
					this.mc._y  = this.mc._parent._ymouse + this.yo;		
				}
			};
		}
		
	}
	static function delfollow(mc:MovieClip, obj:Object):Void { 
		_root['mpo'].trace('监视 删除 控制器 '+mc.follow_ctrl)
		_root['mpo'].trace('监视 删除 控制器 '+mc.follow_ctrl.onEnterFrame)
		delete mc.follow_ctrl.onEnterFrame;
		removeMovieClip(mc.follow_ctrl);
		//_root['mpo'].trace(' -------------------------- ' ) 
		_root['mpo'].trace('监视 删除 控制器 '+mc.follow_ctrl)
		_root['mpo'].trace('监视 删除 控制器 '+mc.follow_ctrl.onEnterFrame)
	}
	static function doFollowTW(a:MovieClip, obj:Object):Void {
		var d:String = obj.d;
		var limit:Array = obj.limit;
		var inside:Function = obj.inside;
		var outside:Function = obj.outside;
		//
		if (limit != null && limit.length != 2) {
			trace('驳回!! 请为 limit 正型');
			return;
		}
		if (d == null) {
			a.onEnterFrame = function() {
				this._x += (this._parent._xmouse-this._x)*.3;
				this._y += (this._parent._ymouse-this._y)*.3;
			};
			return;
		}
		a.d = d;
		if (limit == null) {
			a.onEnterFrame = function() {
				this[d] += (this._parent[d+'mouse']-this[d])*.3;
			};
			return;
		}
		// USAGE       
		//		AS2.EffoM.follow(girl_mc,{d:'_x', limit:[50, 404], inside:function () {
		//Mouse.hide();
		//}, outside:function () {
		//Mouse.show();
		//}});
		a.el = limit;
		a.ei = inside;
		a.eo = outside;
		a.es = null;
		a.onEnterFrame = function() {
			// KEEP FOR EVER !! 
			// var m = Math.max(el[0], Math.min(el[1], this._parent[d+'mouse']));
			var m = this._parent[d+'mouse'];
			var el = this.el;
			var ei = this.ei;
			var eo = this.eo;
			var es = this.es;
			this[d] += (Math.max(el[0], Math.min(el[1], this._parent[d+'mouse']))-this[d])*.3;
			if ((m<el[0] || m>el[1]) && (es == null || es == 'in')) {
				es = 'out';
				eo();
			} else if (es == null || es == 'out') {
				es = 'in';
				ei();
			}
		};
		// **********************************
		// KEEP FOR EVER !! 
		// VERSION 1
		/*else if (d == 'x') {
		if (limit == null) {
		a.onEnterFrame = function() {
		this._x += (this._parent._xmouse-this._x)*.3;
		};
		} else {
		a.limit = limit;
		a.onEnterFrame = function() {
		var m = Math.max(limit[0], this._parent._xmouse);
		m = Math.min(limit[1], m);
		this._x += (m-this._x)*.3;
		};
		}
		} else if (d == 'y') {
		if (limit == null) {
		a.onEnterFrame = function() {
		this._y += (this._parent._ymouse-this._y)*.3;
		};
		} else {
		a.limit = limit;
		a.onEnterFrame = function() {
		var m = Math.max(limit[0], this._parent._ymouse);
		m = Math.min(limit[1], m);
		this._y += (m-this._y)*.3;
		};
		}
		}*/
	}
	public static function isClick(mc:MovieClip, _obj:Object):Object {
		var obj:Object = (_obj != null)?_obj:{};  
		obj.onMouseDown = Delegate.create(obj, function () {
			if (mc.hitTest(_root._xmouse, _root._ymouse)) { 
				//trace("A2.io.Rat::isClick = " )
				this.callback({it:mc,id:A2.btnEffect.uBtn.getLastID(mc._name)});
			}
		});
		Mouse.addListener(obj);
		return obj;
	}
	public static function onClick(mc:MovieClip,_obj:Object):Object {
		return isClick(mc,_obj)
	}
	public static function delClick(_obj:Object):Boolean {
		return delMouseCtrl(_obj);
	}
	public static function delMouseCtrl(_obj:Object):Boolean {
		Mouse.removeListener(_obj);
		_obj = null;
		delete _obj;
		return true;
	}
	public	static function delOver(mc) {
		
		Mouse.removeListener(mc.RatOver_obj);
		delete mc.RatOver_obj;
		mc.RatOver_obj = null;
	}
	public static function pauseOver(a:MovieClip, obj:Object) { 
		
	}
	public static function doOver(scope:MovieClip,mc:MovieClip, obj:Object) {
		// obj.onIN
		// obj.onOUT
		// obj.autoHidden
		if (scope.constructor != MovieClip)  throw new Error("!!!--Error-- Rat::doOver = scope NOT MC")
		
		if (obj == null) {
			var obj = {};
		}
		obj.a = mc;
		obj.m = 0;
		obj.autoHidden = (obj.autoHidden == undefined) ? true : obj.autoHidden;
		var onIN = obj.onIN;
		var onOUT = obj.onOUT;
		// KEEP FOR EVER !!        
		//a.onEnterFrame = Delegate.create(obj, function () {
		obj.onMouseMove = Delegate.create(obj, function () {
			if (mc.hitTest(_root._xmouse, _root._ymouse,true) && (this.m == 0)) {
				if (this.autoHidden) Mouse.hide(); 
				 Delegate.create(scope,onIN)({it:mc});
				this.m = 1;
			} else if (!mc.hitTest(_root._xmouse, _root._ymouse,true) && (this.m == 1)) {
				if (this.autoHidden) Mouse.show();
				Delegate.create(scope,onOUT)({it:mc});
				this.m = 0;
			}
			updateAfterEvent();
		});
		/*
		var ctrl = MC.newMC();
		ctrl.onEnterFrame = Delegate.create(obj, function() {
			if (!a.hitTest(_root._xmouse, _root._ymouse) && (this.m == 1)) {
				if (this.auto) Mouse.show();
				this.onOUT({it:a});
				this.m = 0;
			}			
		})*/
		Mouse.addListener(obj);
		//a.EffoM_=obj;
		//mc.RatOver_obj=obj
		return obj;
	}
	static function equal(a:MovieClip):Void {
		var obj = {};
		obj.onMouseMove = Delegate.create(a, function () {
			a._x = a._parent._x;
			a._y = a._parent._y;
		});
	}
	static function del(a) {
		delete a.onEnterFrame;
	}
}
